package com.penghaonan.appframework.utils.monitor;

import android.content.Context;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.os.Handler;
import android.os.Looper;

import com.penghaonan.appframework.AppDelegate;

public class SoundMonitor extends AMonitor<Integer> {
    AudioManager audioManager;
    private final ContentObserver observer = new ContentObserver(new Handler(Looper.getMainLooper())) {
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            notifyListeners();
        }
    };

    public SoundMonitor() {
        audioManager = (AudioManager) AppDelegate.getApp().getSystemService(Context.AUDIO_SERVICE);
        AppDelegate.getApp().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, observer);
    }

    @Override
    public Integer getValue() {
        return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    }

    public Integer getMaxValue() {
        return audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    }

    @Override
    public void release() {
        super.release();
        AppDelegate.getApp().getContentResolver().unregisterContentObserver(observer);
    }
}
